package com.yili99.services;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Service;

@Service
public class FileServices {

	public boolean saveFile(ServletContext context, InputStream is, String fileName) {
		//String uploadPath = context.getRealPath("\\") + "uploadPic\\" + fileName;
		//获取upload文件夹得真实路径  
	    String realpath = "d:/";
	    System.out.println(realpath); 
	    boolean success = false;
		
		FileOutputStream outputStream = null;
		try {
			File file = new File(realpath+"/"+fileName);
			if(!file.exists()){
				file.createNewFile();
			}
			outputStream = new FileOutputStream(file);
			int count = 0;
			byte[] buffer = new byte[1024 * 8];
			while ((count = is.read(buffer)) > 0) {
				outputStream.write(buffer, 0, count);
			}
			success = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}

				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return success;
	}

}
